

package com.jf.cloud.supplier.service.impl;

import com.jf.cloud.common.database.dto.PageDTO;
import com.jf.cloud.common.database.util.PageUtil;
import com.jf.cloud.common.database.vo.PageVO;
import com.jf.cloud.common.exception.LuckException;
import com.jf.cloud.common.security.AuthUserContext;
import com.jf.cloud.supplier.constant.AuditStatus;
import com.jf.cloud.supplier.dto.SupplierCompanyAuditingDTO;
import com.jf.cloud.supplier.dto.SupplierCompanyDTO;
import com.jf.cloud.supplier.mapper.CompanyAuditingMapper;
import com.jf.cloud.supplier.model.SupplierCompany;
import com.jf.cloud.supplier.model.SupplierCompanyAuditing;
import com.jf.cloud.supplier.service.CompanyAuditingService;
import com.jf.cloud.supplier.service.SupplierCompanyService;
import com.jf.cloud.supplier.vo.SupplierCompanyAuditingVO;
import com.jf.cloud.supplier.vo.SupplierCompanyVO;
import ma.glasnost.orika.MapperFacade;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * @author zz
 * @date 2022/9/13 14:40
 */

@Service
public class CompanyAuditingServiceImpl implements CompanyAuditingService {

    @Autowired
    private CompanyAuditingMapper companyAuditingMapper;
    @Autowired
    private SupplierCompanyService supplierCompanyService;
    @Autowired
    private MapperFacade mapperFacade;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void applyChangeCompanyInfo(SupplierCompanyDTO supplierCompanyDTO) {
        // 把之前审核不通过的工商信息删除
        supplierCompanyService.deleteBySupplierIdAndStatus(supplierCompanyDTO.getSupplierId(), AuditStatus.FAILAUDIT.value());
        // 保存变更工商信息
        SupplierCompany supplierCompany = mapperFacade.map(supplierCompanyDTO, SupplierCompany.class);
        supplierCompany.setStatus(AuditStatus.WAITAUDIT.value());
        supplierCompanyService.save(supplierCompany, false);
        // 生成审核信息
        generateAuditInformation(supplierCompany);
    }

    @Override
    public SupplierCompanyAuditingVO getLatestAuditingBySupplierId(Long supplierId) {
        return companyAuditingMapper.getLatestAuditingBySupplierId(supplierId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void revoke(Long supplierId) {

        SupplierCompanyAuditingVO companyAuditingVO = getLatestAuditingBySupplierId(supplierId);
        if (Objects.isNull(companyAuditingVO) || !Objects.equals(companyAuditingVO.getStatus(), AuditStatus.WAITAUDIT.value())) {
            throw new LuckException("申请信息未能找到，请刷新页面");
        }
        // 删除申请变更的工商信息
        supplierCompanyService.deleteBySupplierIdAndStatus(supplierId, AuditStatus.WAITAUDIT.value());
        // 删除申请审核信息
        companyAuditingMapper.deleteById(companyAuditingVO.getCompanyAuditingId());
    }

    @Override
    public PageVO<SupplierCompanyAuditingVO> page(PageDTO pageDTO, SupplierCompanyAuditingDTO supplierCompanyAuditingDTO) {
        return PageUtil.doPage(pageDTO, () -> companyAuditingMapper.list(supplierCompanyAuditingDTO));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void audit(SupplierCompanyAuditingDTO supplierCompanyAuditingDTO) {
        SupplierCompanyAuditing companyAuditing = companyAuditingMapper.getById(supplierCompanyAuditingDTO.getCompanyAuditingId());
        if (Objects.isNull(companyAuditing) || !Objects.equals(companyAuditing.getStatus(), AuditStatus.WAITAUDIT.value())) {
            throw new LuckException("申请信息未能找到，请刷新页面");
        }
        companyAuditing.setAuditorId(AuthUserContext.get().getUserId());
        // 审核不通过
        if (Objects.equals(supplierCompanyAuditingDTO.getStatus(), AuditStatus.FAILAUDIT.value())) {
            companyAuditing.setStatus(AuditStatus.FAILAUDIT.value());
            companyAuditing.setRemarks(supplierCompanyAuditingDTO.getRemarks());
            companyAuditingMapper.update(companyAuditing);
            // 把对应的工商信息设置为审核不通过状态
            SupplierCompany supplierCompany = new SupplierCompany();
            supplierCompany.setSupplierCompanyId(companyAuditing.getSupplierCompanyId());
            supplierCompany.setStatus(AuditStatus.FAILAUDIT.value());
            supplierCompanyService.updateById(supplierCompany);
            return;
        }
        // 审核通过
        if (Objects.equals(supplierCompanyAuditingDTO.getStatus(), AuditStatus.SUCCESSAUDIT.value())) {
            companyAuditing.setStatus(AuditStatus.SUCCESSAUDIT.value());
            companyAuditingMapper.update(companyAuditing);

            supplierCompanyService.batchUpdateStatus(companyAuditing.getSupplierId(), AuditStatus.FAILAUDIT.value());
            // 把对应的工商信息设置为启用状态
            SupplierCompany supplierCompany = new SupplierCompany();
            supplierCompany.setSupplierCompanyId(companyAuditing.getSupplierCompanyId());
            supplierCompany.setStatus(AuditStatus.SUCCESSAUDIT.value());
            supplierCompanyService.updateById(supplierCompany);
        }

    }

    @Override
    public SupplierCompanyAuditingVO getAuditInfo(Long supplierId) {
        SupplierCompanyAuditingVO companyAuditingVO = getLatestAuditingBySupplierId(supplierId);
        if (Objects.isNull(companyAuditingVO)) {
            return companyAuditingVO;
        }
        SupplierCompanyVO supplierCompanyVO = supplierCompanyService.getById(companyAuditingVO.getSupplierCompanyId());
        companyAuditingVO.setSupplierCompanyVO(supplierCompanyVO);
        return companyAuditingVO;
    }

    /**
     * 生成店铺申请记录
     *
     * @param supplierCompany
     */
    private void generateAuditInformation(SupplierCompany supplierCompany) {
        SupplierCompanyAuditing supplierCompanyAuditing = new SupplierCompanyAuditing();
        supplierCompanyAuditing.setSupplierId(supplierCompany.getSupplierId());
        supplierCompanyAuditing.setSupplierCompanyId(supplierCompany.getSupplierCompanyId());
        supplierCompanyAuditing.setUserId(AuthUserContext.get().getUserId());
        supplierCompanyAuditing.setStatus(AuditStatus.WAITAUDIT.value());
        companyAuditingMapper.insert(supplierCompanyAuditing);
    }
}
